import { defineStore } from "pinia";
import { retry, Subject } from "rxjs";
import { VIEW_MODE } from "@/FrontProcessing";
import { _global } from ".";

export interface ProjectInfo {
  id: string;
  name: string;
  projectContent: {
    url: string;
    version: string;
  };
  createAt: string;
  updateAt: string;
}

export const useCoreStore = defineStore("core", {
  state: () => {
    return {
      Notify: new Subject<any>(),
      measureModule: {
        on: false,
      },
      bomInfo: {} as any,
      elements: [] as Array<{ num: number; name: string }>,
      filename: "" as string,
      fileLoadModalActive: false,
      fileSaveModalActive: false,
      exportModalActive: false,
      GridModalVueActive: false,
      WasherModalVueActive: false,
      TetrahedronModalVueActive: false,
      MeshCheckModalVueActive: false,
      ElemEditModalVueActive: false,
      FreeEdgeCheckModalVueActive: false,
      ColorModalValueActive: false,
      RemeshModalVueActive: false,
      importModelVueActive: false,
      wrapperModalVueActive: false,
      reNumberActiveModal: false,
      exportModelVueActive: false,
      DeleteEntityModalVueActive: false,
      CounterBoreModalVueActive: false,
      MidSurfaceModalVueActive: false,
      solverTaskModelVueActive: false,
      solverCardModelVueActive: false,
      organizedModalActive: false,
      loadStepModelVueActive: false,
      importFileModalVueActive: false,
      outputFileModalVueActive: false,
      layerEditorModalActive: false,
      edgePartitionModalVueActive: false,
      transformModalVueActive: false,
      solverResultModelVueActive: false,
      importCloudFileModalVueActive: false,
      meshQualityModalActive: false,

      trimModalVueActive: false,
      normalVectorModalVueActive: false,
      normalVectorModalVueValue: {},

      freeBodyModalVueActive: false,
      freeBodyModalVueValue: {},
      collectionCardModalVueActive: false,
      physicalFieldModalVueActive: false,
      physicalFieldModalVueValue: {},

      // * 数巧云文件选择弹窗
      simrightFileSelectModalActive: false,
      solverResultName: "",
      solverResultId: "",

      isSurfaceMode: true,
      isLineMode: false,
      isPointMode: false,
      isEdgeMode: false,
      connectModalActive: true,
      chamferFaceModalVueActive: false,
      chamferEdgeModalVueActive: false,
      chamferAngleModalVueActive: false,
      CFDMeshActiveModalVueActive: false,
      offsetActiveModalVueActive: false,
      nameRuleVueActive: false,
      findForceVueActive: false,
      username: "web_client",
      //@ts-ignore
      serverUrl: import.meta.env.VITE_APP_ENGINE_DEFAULT_ORIGIN,
      //@ts-ignore
      serverPath: import.meta.env.VITE_APP_ENGINE_DEFAULT_PATH,
      projectInfo: {} as ProjectInfo,
      // * 组件视图 "前处理" "求解器" "后处理"
      componentView: "前处理",
      // * 材料选择弹窗
      materialSelectModalActive: false,
      // * 云资源选择弹窗
      cloudResourceSelectModalActive: false,
      // * 当前选中云资源
      currentCloudResource: "",
      // * 当前模型库选中资源地址
      currentLibraryResource: "",
      // * 结果库选择弹窗
      resultLibraryModalActive: false,
      // * 当前结果库选中资源地址
      currentResultLibraryResource: "",
      geometricPruning: false,
      edgeMergeModal: false,
    };
  },
  actions: {
    setEngine(v: any) {
      _global.Engine = v;
    },
    setEngineState(value: number) {
      _global.Engine.state.set("Mode", value);
    },

    getEngineState() {
      return _global.Engine.state.get("Mode");
    },
    setViewMode(v: VIEW_MODE) {
      _global.Engine.state.set("ViewMode", v);
    },
    setEdgeMode(v: boolean) {
      this.isEdgeMode = v;
    },
    measureModuleOn() {
      _global.Engine.state.set("Mode", 1);
      this.measureModule.on = true;
    },
    measureModuleOff() {
      _global.Engine.state.set("Mode", 0);
      this.measureModule.on = false;
    },
    setFilename(v: string) {
      this.filename = v;
    },
    getFilename() {
      return this.filename;
    },
    setBomInfo(v: any) {
      this.bomInfo = v;
    },
    setElements() {
      _global.Engine.card.getElements().then((res) => {
        this.elements = res;
      });
    },
    setFileLoadModalActive(v: boolean) {
      this.fileLoadModalActive = v;
    },
    setConnectModalActive(v: boolean) {
      this.connectModalActive = v;
    },
    setFileSaveModalActive(v: boolean) {
      this.fileSaveModalActive = v;
    },
    setOffsetActiveModal(v: boolean) {
      this.offsetActiveModalVueActive = v;
    },
    setWrapperModalActive(v: boolean) {
      this.wrapperModalVueActive = v;
    },
    setOrganizedVueActive(v: boolean) {
      this.organizedModalActive = v;
    },
    setExportModalActive(v: boolean) {
      this.exportModalActive = v;
    },
    setreNumberActiveModal(v: boolean) {
      this.reNumberActiveModal = v;
    },
    setCFDGridModalVueActive(v: boolean) {
      this.CFDMeshActiveModalVueActive = v;
    },
    setGridModalVueActive(v: boolean) {
      this.GridModalVueActive = v;
    },
    setEdgePartitionModalVueActive(v: boolean) {
      this.edgePartitionModalVueActive = v;
    },
    setTransformnModalVueActive(v: boolean) {
      this.transformModalVueActive = v;
    },
    setWasherModalVueActive(v: boolean) {
      this.WasherModalVueActive = v;
    },
    setTetrahedronModalVueActive(v: boolean) {
      this.TetrahedronModalVueActive = v;
    },
    setDeleteEntityModalVueActive(v: boolean) {
      this.DeleteEntityModalVueActive = v;
    },
    setCounterBoreModalVueActive(v: boolean) {
      this.CounterBoreModalVueActive = v;
    },
    setMidSurfaceModalVueActive(v: boolean) {
      this.MidSurfaceModalVueActive = v;
    },
    setGeometricPruningModalVueActive(v: boolean) {
      this.geometricPruning = v;
    },
    setEdgeMergeModalVueActive(v: boolean) {
      this.edgeMergeModal = v;
    },

    setMeshCheckModalVueActive(v: boolean) {
      this.MeshCheckModalVueActive = v;
    },
    setElemEditModalVueActive(v: boolean) {
      this.ElemEditModalVueActive = v;
    },
    setFreeEdgeCheckModalVueActive(v: boolean) {
      this.FreeEdgeCheckModalVueActive = v;
    },
    setColorModalVueActive(v: boolean) {
      this.ColorModalValueActive = v;
    },
    setRemeshModalVueActive(v: boolean) {
      this.RemeshModalVueActive = v;
    },
    setExportModelVueActive(v: boolean) {
      this.exportModelVueActive = v;
    },
    setSolverTaskModelVueActive(v: boolean) {
      this.solverTaskModelVueActive = v;
    },
    setSolverCardModelVueActive(v: boolean) {
      this.solverCardModelVueActive = v;
    },
    setSolverResultModelVueActive(v: boolean) {
      this.solverResultModelVueActive = v;
    },
    setSolverResultName(v: string) {
      this.solverResultName = v;
    },
    setSolverResultId(v: string) {
      this.solverResultId = v;
    },
    setLoadStepModelVueActive(v: boolean) {
      this.loadStepModelVueActive = v;
    },
    setImportFileModalVueActive(v: boolean) {
      this.importFileModalVueActive = v;
    },
    setImportCloudFileModalVueActive(v: boolean) {
      this.importCloudFileModalVueActive = v;
    },
    setOutputFileModalVueActive(v: boolean) {
      this.outputFileModalVueActive = v;
    },
    setImportModelVueActive(v: boolean) {
      this.importModelVueActive = v;
    },
    setSurfaceMode(v: boolean) {
      this.isSurfaceMode = v;
    },
    setLineMode(v: boolean) {
      this.isLineMode = v;
    },
    setPointMode(v: boolean) {
      this.isPointMode = v;
    },
    setTrimModalVueActive(v: boolean) {
      this.trimModalVueActive = v;
    },
    setNormalVectorModalVueActive(v: boolean) {
      this.normalVectorModalVueActive = v;
    },
    setNormalVectorModalVueValue(value: Record<string, any>) {
      this.normalVectorModalVueValue = JSON.stringify(value);
    },

    setFreeBodyModalVueActive(v: boolean) {
      this.freeBodyModalVueActive = v;
    },
    setFreeBodyModalVueValue(value: Record<string, any>) {
      this.freeBodyModalVueValue = JSON.stringify(value);
    },

    setCollectionCardModalVueActive(v: boolean) {
      this.collectionCardModalVueActive = v;
    },
    setPhysicalFieldModalVueActive(v: boolean) {
      this.physicalFieldModalVueActive = v;
    },
    setPhysicalFieldModalVueValue(value: Record<string, any>) {
      this.physicalFieldModalVueValue = JSON.stringify(value);
    },
    setChamferFaceModalVueActive(v: boolean) {
      this.chamferFaceModalVueActive = v;
    },
    setChamferEdgeModalVueActive(v: boolean) {
      this.chamferEdgeModalVueActive = v;
    },
    setChamferAngleModalVueActive(v: boolean) {
      this.chamferAngleModalVueActive = v;
    },
    setNameRuleVueActive(v: boolean) {
      this.nameRuleVueActive = v;
    },
    /**
     * 设置工程信息
     * @param v {ProjectInfo}
     */
    setProjectInfo(v: ProjectInfo) {
      this.projectInfo = v;
    },

    /**
     * @description 设置组件视图
     * @param v {string} "前处理" "求解器" "后处理"
     */
    changeComponentView(v: string) {
      this.componentView = v;
    },
    /**
     * @description 设置材料选择弹窗
     * @param v {boolean}
     */
    changeMaterialSelectModalActive(v: boolean) {
      this.materialSelectModalActive = v;
    },
    /**
     * @description 设置云资源选择弹窗
     * @param v {boolean}
     */
    changeCloudResourceSelectModalActive(v: boolean) {
      this.cloudResourceSelectModalActive = v;
    },
    changeCurrentCloudResource(v: string) {
      this.currentCloudResource = v;
    },
    openSimRightModal() {
      this.simrightFileSelectModalActive = true;
    },
    closeSimRightModal() {
      this.simrightFileSelectModalActive = false;
    },
    setCurrentLibraryResource(v: string) {
      this.currentLibraryResource = v;
    },
    updateResultLibraryModalActive(v: boolean) {
      this.resultLibraryModalActive = v;
    },
    setCurrentResultLibraryResource(v: string) {
      this.currentResultLibraryResource = v;
    },
    setMeshQualityModalVueActive(v: boolean) {
      this.meshQualityModalActive = v;
    },
  },
});
